﻿using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcSearchNearestNavPoint : AIBaseNode
    {
        protected override void OnExecute()
        {
            if ((_entity.data.npcStyle & NpcStyle.PathWalker) <= 0)
            {
                EndAction(false);
                return;
            }
            
            _entity.seeker.SwitchSpeedStrategy(SpeedStrategyType.Const);
            Vector3 pos = _transform.position;
            Vector3 forward = _transform.forward;
            PathPointData point = LibertyAIUtils.TrafficManager.PathManager.GetNearestNavPoint(pos, forward, out PathDirection pathDirection);
            float offsetFactor = _entity.data.pathOffsetFactor;

            if (point!=null)
            {
                Vector3 destination = AIGraphUtils.GetNpcDestinationFormPoint(point, GetPosition(), offsetFactor);
                
                _entity.data.destination = destination;
                _entity.data.pathId = point.pathId;
                _entity.data.pathPointIndex = point.pointIndex;
                SetDestination(destination);
            }
            EndAction(true);
        }
        
    }
}